package cn.com.cym.gamezao.service.recruit.impl;

import cn.com.cym.gamezao.base.BaseImpl;
import cn.com.cym.gamezao.base.BaseRepository;
import cn.com.cym.gamezao.base.ResultResponse;
import cn.com.cym.gamezao.base.SearchPage;
import cn.com.cym.gamezao.entity.authorize.User;
import cn.com.cym.gamezao.entity.xmu.recruit.ApplyLog;
import cn.com.cym.gamezao.entity.xmu.recruit.UserApply;
import cn.com.cym.gamezao.exception.BaseException;
import cn.com.cym.gamezao.jwt.RequestUser;
import cn.com.cym.gamezao.repository.xmu.recruit.ApplyLogRepository;
import cn.com.cym.gamezao.repository.xmu.recruit.UserApplyRepository;
import cn.com.cym.gamezao.service.recruit.ApplyLogService;
import cn.com.cym.gamezao.util.CommonUtils;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class ApplyLogImpl extends BaseImpl<ApplyLog> implements ApplyLogService {

    final ApplyLogRepository thisRepository;
    final UserApplyRepository userApplyRepository;

    ApplyLogImpl(ApplyLogRepository thisRepository,UserApplyRepository userApplyRepository){
        this.thisRepository = thisRepository;
        this.userApplyRepository = userApplyRepository;
    }

    @Override
    public List<ApplyLog> getAllByUserApplyId(Long userApplyId) {
        final UserApply userApply = userApplyRepository.getById(userApplyId);
        thisRepository.findAllByUserApply(userApply);
        return thisRepository.findAllByUserApply(userApply);
    }

    @Override
    public BaseRepository<ApplyLog, Long> getJpaRepository() {
        return thisRepository;
    }

    @Override
    public ApplyLog save(ApplyLog applyLog) {
        User user  = RequestUser.get();
        // 判断该用户是否有审核权限
        if(CommonUtils.isNotEmpty(user.getUserType())){
            throw new BaseException("该用户无审核权限",new ResultResponse("该用户无审核权限", HttpStatus.BAD_REQUEST.value()));
        }
        applyLog.setUser(user);
        // 修改主表数据
        userApplyRepository.save(applyLog.getUserApply().setApplyStatus(applyLog.getApplyStatus()));
        return super.save(applyLog);
    }

    @Override
    public Page<ApplyLog> findByPageable(SearchPage searchPage) {
        return null;
    }
}
